(function(){

'use strict';

angular.module('controllers.faultLocation.pbossOm')
  .controller('productManagementController', productManagementController);

function productManagementController ($scope, pbossOmServer,$filter,COREDATA,CoreService,CoreLoading) {
	$scope.flag = false;
	$scope.shoumun = 1;
    $scope.provinces = COREDATA.PROVINCES;
    $scope.errorInfo = COREDATA.ERRORINFO; //日期错误信息
	$scope.translate = {
		provinceCodes: [],
		startTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 31),
		endTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1)
	};

	$scope.queryProductData = function(){
		var paramsData = {};
		var provinceCodes;
		$scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
		provinceCodes = $scope.translate.provinceCodes.map(function(item){
          return item.provinceCode;
		});
		paramsData.provinceCodes = provinceCodes;
		var startTime = $filter('date')($scope.translate.startTime,'yyyyMMdd');
		var endTime = $filter('date')($scope.translate.endTime,'yyyyMMdd');
		paramsData.startTime = $filter('date')($scope.translate.startTime,'yyyy-MM-dd');
		paramsData.endTime = $filter('date')($scope.translate.startTime,'yyyy-MM-dd');
		$scope.time = startTime+'-'+endTime;
		if($scope.shoumun === 0){
			CoreLoading.setState(true);
			pbossOmServer.queryProdoct(paramsData,queryProductData);
		}
	};
	$scope.resetValue = function(){
		$scope.translate = {
			provinceCodes: [],
			startTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 31),
			endTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1)
		};
	};

	var productEchart = echarts.init(document.getElementById('product'));
	
  function queryProductData(res) {
  	  CoreLoading.setState(false);
	  if(res.state === '10000' && res.data.pageData.length > 0 ){
		  $scope.flag = true;
		  $scope.flagObj = true;
		  $scope.productData = res.data.pageData;

		  var nameArr = [];
		  var offNumArr = [];
		  var onNumArr = [];
		  angular.forEach($scope.productData,function(itemData){
			  offNumArr.push(itemData.quantityShelves);
			  onNumArr.push(itemData.reactQuantity);
			  nameArr.push(itemData.provinceName);
		  });
		  var itemStyle =  {
			  normal: { // 系列级个性化，横向渐变填充
				  borderRadius: 5,
				  label: {
					  show: true,
					  textStyle: {
						  fontSize: '10',
						  fontFamily: '微软雅黑',
						  fontWeight: 'bold'
					  }
				  }
			  }
		  };
		  var option = {
			  title: {
				  text: '各省份产品下架数/生效统计数统计_'+$scope.time,
				  x: 'center'
			  },
			  tooltip: {
				  trigger: 'axis',
				  axisPointer: {
					  type: 'shadow'
				  }
			  },
			  legend: {
				  orient: 'vertical',
				  x: 'right',
				  y: 'center',
				  data: ['下架产品数','生效产品数']
			  },
			  calculable: true,
			  grid: {
				  x2: '12%'
			  },
			  xAxis: [
				  {
					  type: 'category',
					  data: nameArr
				  }
			  ],
			  yAxis: [
				  {
					  type: 'value'
				  }
			  ],
			  series: [
				  {
					  name: '下架产品数',
					  type: 'bar',
					  barMaxWidth:'50',
					  itemStyle: itemStyle,
					  data: offNumArr
				  },
				  {
					  name: '生效产品数',
					  type: 'bar',
					  barMaxWidth:'50',
					  itemStyle: itemStyle,
					  data: onNumArr
				  }
			  ]
		  };
          productEchart.clear();
		  productEchart.setOption(option);
		  exportExcel();

	  }
  }
  function exportExcel(){
  	$scope.outData = {
  		startTime: $scope.startTime,
  		endTime: $scope.endTime,
  		title: '各省份产品下架数/生效统计数统计_'+$scope.time
  	};
  	$scope.exportUrl = COREDATA.FORMPREFIX + 'OMdisplay/PBossOM/businessReportAnalysis/productManagement/export'; //表导出的请求地址

  }
  $scope.queryProductData();
}
})();